#include "common.h"

#include <SDL/SDL.h>

keynum_t com_translate_key(SDL_keysym * keysym)
{
	int sdl_key = keysym->sym;
	keynum_t key = 0;

	if(sdl_key >= SDLK_SPACE && sdl_key < SDLK_DELETE) {
		key = (keynum_t)sdl_key;
	} else {
		switch(sdl_key)
		{
			case SDLK_PAGEUP:       key = K_PGUP;          break;
			case SDLK_KP9:          key = K_KP_PGUP;       break;
			case SDLK_PAGEDOWN:     key = K_PGDN;          break;
			case SDLK_KP3:          key = K_KP_PGDN;       break;
			case SDLK_KP7:          key = K_KP_HOME;       break;
			case SDLK_HOME:         key = K_HOME;          break;
			case SDLK_KP1:          key = K_KP_END;        break;
			case SDLK_END:          key = K_END;           break;
			case SDLK_KP4:          key = K_KP_LEFTARROW;  break;
			case SDLK_LEFT:         key = K_LEFTARROW;     break;
			case SDLK_KP6:          key = K_KP_RIGHTARROW; break;
			case SDLK_RIGHT:        key = K_RIGHTARROW;    break;
			case SDLK_KP2:          key = K_KP_DOWNARROW;  break;
			case SDLK_DOWN:         key = K_DOWNARROW;     break;
			case SDLK_KP8:          key = K_KP_UPARROW;    break;
			case SDLK_UP:           key = K_UPARROW;       break;
			case SDLK_ESCAPE:       key = K_ESCAPE;        break;
			case SDLK_KP_ENTER:     key = K_KP_ENTER;      break;
			case SDLK_RETURN:       key = K_ENTER;         break;
			case SDLK_TAB:          key = K_TAB;           break;
			case SDLK_F1:           key = K_F1;            break;
			case SDLK_F2:           key = K_F2;            break;
			case SDLK_F3:           key = K_F3;            break;
			case SDLK_F4:           key = K_F4;            break;
			case SDLK_F5:           key = K_F5;            break;
			case SDLK_F6:           key = K_F6;            break;
			case SDLK_F7:           key = K_F7;            break;
			case SDLK_F8:           key = K_F8;            break;
			case SDLK_F9:           key = K_F9;            break;
			case SDLK_F10:          key = K_F10;           break;
			case SDLK_F11:          key = K_F11;           break;
			case SDLK_F12:          key = K_F12;           break;
			case SDLK_F13:          key = K_F13;           break;
			case SDLK_F14:          key = K_F14;           break;
			case SDLK_F15:          key = K_F15;           break;

			case SDLK_BACKSPACE:    key = K_BACKSPACE;     break;
			case SDLK_KP_PERIOD:    key = K_KP_DEL;        break;
			case SDLK_DELETE:       key = K_DEL;           break;
			case SDLK_PAUSE:        key = K_PAUSE;         break;

			case SDLK_LSHIFT:
			case SDLK_RSHIFT:       key = K_SHIFT;         break;

			case SDLK_LCTRL:
			case SDLK_RCTRL:        key = K_CTRL;          break;

			case SDLK_RMETA:
			case SDLK_LMETA:        key = K_COMMAND;       break;

			case SDLK_RALT:
			case SDLK_LALT:         key = K_ALT;           break;

			case SDLK_LSUPER:
			case SDLK_RSUPER:       key = K_SUPER;         break;

			case SDLK_KP5:          key = K_KP_5;          break;
			case SDLK_INSERT:       key = K_INS;           break;
			case SDLK_KP0:          key = K_KP_INS;        break;
			case SDLK_KP_MULTIPLY:  key = K_KP_STAR;       break;
			case SDLK_KP_PLUS:      key = K_KP_PLUS;       break;
			case SDLK_KP_MINUS:     key = K_KP_MINUS;      break;
			case SDLK_KP_DIVIDE:    key = K_KP_SLASH;      break;

			case SDLK_MODE:         key = K_MODE;          break;
			case SDLK_COMPOSE:      key = K_COMPOSE;       break;
			case SDLK_HELP:         key = K_HELP;          break;
			case SDLK_PRINT:        key = K_PRINT;         break;
			case SDLK_SYSREQ:       key = K_SYSREQ;        break;
			case SDLK_BREAK:        key = K_BREAK;         break;
			case SDLK_MENU:         key = K_MENU;          break;
			case SDLK_POWER:        key = K_POWER;         break;
			case SDLK_EURO:         key = K_EURO;          break;
			case SDLK_UNDO:         key = K_UNDO;          break;
			case SDLK_SCROLLOCK:    key = K_SCROLLOCK;     break;
			case SDLK_NUMLOCK:      key = K_KP_NUMLOCK;    break;
			case SDLK_CAPSLOCK:     key = K_CAPSLOCK;      break;

			default:
				if(sdl_key >= SDLK_WORLD_0 && sdl_key <= SDLK_WORLD_95)
					key = (sdl_key - SDLK_WORLD_0) + K_WORLD_0;
				break;
		}
	}

	return key;
}

//键盘鼠标事件队列
#define MAX_QUEUED_EVENTS 256
#define MASK_QUEUED_EVENTS (MAX_QUEUED_EVENTS-1)
static event_t	event_queue[MAX_QUEUED_EVENTS];
static int		event_head = 0;
static int		event_tail = 0;

void com_queue_event(int type, int value1, int value2)
{
	if (event_tail - event_head >= MAX_QUEUED_EVENTS) {
		com_warning("event queue is overflow");
	} else {
		event_t * ev = &event_queue[event_tail & MASK_QUEUED_EVENTS];

		ev->type = type;
		ev->key = value1;
		ev->down = value2;
		ev->time = SDL_GetTicks();

		event_tail ++;
	}
}

event_t * com_get_event(void)
{
	if (event_tail <= event_head) {
		return NULL;
	} else if (event_head >= MAX_QUEUED_EVENTS) {
		event_head -= MAX_QUEUED_EVENTS;
		event_tail -= MAX_QUEUED_EVENTS;
	}

	return &event_queue[event_head++];
}


